package com.xiaoma.verbal.webservice;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.xiaoma.verbal.dao.PayAudioGrabDao;
import com.xiaoma.verbal.dao.PayAudioMarktipDao;
import com.xiaoma.verbal.dao.PayAudioResultDao;
import com.xiaoma.verbal.domain.PayAudioGrab;
import com.xiaoma.verbal.domain.PayAudioMarktip;
import com.xiaoma.verbal.domain.PayAudioResult;
import com.xiaoma.verbal.dto.CompleteExerciseDto;
import com.xiaoma.verbal.dto.GrabExerciseDto;
import com.xiaoma.verbal.dto.MarkTipDto;

@Controller
@RequestMapping("/service/callback")
public class CallBackWS {

    @Autowired
    private  RestTemplate restTemplate;
    
    @Autowired
    private PayAudioGrabDao payAudioGrabDao;
    
    @Autowired
    private PayAudioResultDao payAudioResultDao;
    
    @Autowired
    private PayAudioMarktipDao payAudioMarktipDao;
    
    @RequestMapping(value="/grabExercise",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> grabExercise(@RequestBody GrabExerciseDto[] dtos) {
        Map<String, Object> result = new HashMap<String, Object>();
        for (GrabExerciseDto item : dtos) {
            PayAudioGrab payAudioGrab = new PayAudioGrab();
            payAudioGrab.setAudioId(item.getAnswerId());
            payAudioGrab.setGrabTime(item.getGrabTime());
            payAudioGrab.setTeacherId(item.getTeacherId());
            payAudioGrabDao.insert(payAudioGrab);
        }
        result.put("status", 0);//成功
        return result;
    }
    
    @RequestMapping(value="/completeExercise",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> completeExercise(@RequestBody CompleteExerciseDto dto) {
        Map<String, Object> result = new HashMap<String, Object>();
        PayAudioResult payAudioResult = new PayAudioResult();
        payAudioResult.setAudioId(dto.getAnswerId());
        payAudioResult.setConvertText(dto.getConvertText());
        payAudioResult.setScore(dto.getScore());
        payAudioResult.setStatus(dto.getStatus());
        payAudioResult.setTeacherId(dto.getTeacherId());
        payAudioResult.setType(dto.getType());
        int id = payAudioResultDao.insert(payAudioResult);
        for (MarkTipDto item : dto.getMarktips()) {
            PayAudioMarktip marktip = new PayAudioMarktip();
            marktip.setAudioResultId(id);
            marktip.setAudioUrl(item.getAudioUrl());
            marktip.setStartIndex(item.getStartIndex());
            marktip.setEndIndex(item.getEndIndex());
            marktip.setTextContent(item.getTextContent());
            payAudioMarktipDao.insert(marktip);
        }
        result.put("status", 0);//成功
        return result;
    }
}
